package top.someapp.dxf2dsvg.jfx;

import javafx.application.Application;
import javafx.scene.Node;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;

import java.lang.ref.WeakReference;

/**
 * @author zwz
 * Created on 2020-04-23
 */
public class JfxApplication extends Application implements JfxContext {
    private static WeakReference<JfxContext> contextRef;
    private Stage stage;
    private Parent root;

    public static void main(String[] args) {
        launch(args);
    }

    public static JfxContext getContext() {
        return contextRef == null ? null : contextRef.get();
    }

    @Override
    public void start(Stage stage) {
        this.stage = stage;
        FxmlLoaderBuilder builder = FxmlLoaderBuilder.make("/jfx/SceneMain.fxml", this);
        root = builder.load();
        stage.setTitle(I18N.getString("app.title"));
        stage.setWidth(800);
        stage.setHeight(600);
        if (root != null) {
            Scene scene = new Scene(root, 300, 250);
            stage.setScene(scene);
        }
        stage.show();
    }

    @Override
    public void init() throws Exception {
        super.init();
        contextRef = new WeakReference<>(this);
    }

    @Override
    public Stage getStage() {
        return stage;
    }

    @SuppressWarnings("unchecked")
    @Override
    public <T extends Node> T getRoot() {
        return (T) root;
    }
}
